﻿Public Class F15_GiaoDichTinDung

    Dim DS As New List(Of CTCaThe)
    Dim t As New TheTinDung

    Private Sub HienThi(ByVal CTCT As List(Of CTCaThe))
        lsv_CTcathe.Items.Clear()
        For Each CT In CTCT
            Dim li As New ListViewItem(CT.Ngay)
            li.Tag = CT
            li.SubItems.Add(CT.Sotien)
            li.SubItems.Add(CT.MoTa)
            lsv_CTcathe.Items.Add(li)
        Next
    End Sub


    Private Sub btnthoatpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub F15_No_Tin_Dung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        t = Me.Tag
        Labelsothe.Text = t.TaiKhoan
        Label4.Text = XL_CTCaThe.No(t.ID)
        DS = XL_CTCaThe.LayDS(t.ID)
        HienThi(DS)
    End Sub

    Private Sub lsv_CTcathe_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsv_CTcathe.SelectedIndexChanged
        Try
            Dim c As CTCaThe = lsv_CTcathe.SelectedItems(0).Tag
            dtpngaychungtupt.Value = c.Ngay
            txtsotien.Text = c.Sotien.ToString()
            rtbm_mota.Text = c.MoTa
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        If (lsv_CTcathe.SelectedItems.Count > 0) Then
            If MessageBox.Show("Ban co muon xoa vi tri da chon ?", "Chu y!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Try
                    Dim c As CTCaThe = lsv_CTcathe.SelectedItems(0).Tag
                    Dim kq As Integer = XL_CTCaThe.Xoa(c)
                    Dim no As Integer = 0
                    If kq <> 0 Then
                        DS.Clear()
                        If RadioButton1.Checked = True Then
                            DS = XL_CTCaThe.LayDS(t.ID)
                            no = XL_CTCaThe.No(t.ID)
                        Else
                            Dim t1 As Date = DateTimePicker1.Value
                            Dim t2 As Date = DateTimePicker2.Value
                            DS = XL_CTCaThe.LayDS2(t.ID, t1, t2)
                            no = XL_CTCaThe.No2(t.ID, t1, t2)
                        End If
                        HienThi(DS)
                        Label4.Text = no
                        MessageBox.Show("Xoa thanh cong!")
                    Else
                        MessageBox.Show("Xoa khong thanh cong")
                    End If
                Catch ex As Exception
                    Exit Sub
                End Try
            End If
        Else
            MessageBox.Show("Ban chua chon vi tri xoa!")
        End If
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        If (lsv_CTcathe.SelectedItems.Count > 0) Then
            Try
                Dim no As Integer = 0
                Dim td As TheTinDung = Me.Tag
                Dim c As CTCaThe = lsv_CTcathe.SelectedItems(0).Tag
                c.Ngay = dtpngaychungtupt.Value.Date
                c.MaTD = td.ID
                c.Sotien = Integer.Parse(txtsotien.Text)
                c.MoTa = rtbm_mota.Text
                Dim kq As Integer = XL_CTCaThe.CapNhat(c)
                If kq <> 0 Then
                    DS.Clear()
                    If RadioButton1.Checked = True Then
                        DS = XL_CTCaThe.LayDS(t.ID)
                        no = XL_CTCaThe.No(t.ID)
                    Else
                        Dim t1 As Date = DateTimePicker1.Value
                        Dim t2 As Date = DateTimePicker2.Value
                        DS = XL_CTCaThe.LayDS2(t.ID, t1, t2)
                        no = XL_CTCaThe.No2(t.ID, t1, t2)
                    End If
                    HienThi(DS)
                    Label4.Text = no
                    MessageBox.Show("Cap nhat thanh cong!")
                Else
                    MessageBox.Show("Cap nhat khong thanh cong")
                End If
            Catch ex As Exception
                MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Exit Sub
            End Try
        Else
            MessageBox.Show("Ban chua chon vi tri cap nhat!")
        End If
    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        Try
            Dim no As Integer = 0
            Dim td As TheTinDung = Me.Tag
            Dim c As New CTCaThe()
            c.Ngay = dtpngaychungtupt.Value.Date
            c.MaTD = td.ID
            c.Sotien = Integer.Parse(txtsotien.Text)
            c.MoTa = rtbm_mota.Text
            Dim kq As Integer = XL_CTCaThe.Them(c)
            If kq <> 0 Then
                DS.Clear()
                If RadioButton1.Checked = True Then
                    DS = XL_CTCaThe.LayDS(t.ID)
                    no = XL_CTCaThe.No(t.ID)
                Else
                    Dim t1 As Date = DateTimePicker1.Value
                    Dim t2 As Date = DateTimePicker2.Value
                    DS = XL_CTCaThe.LayDS2(t.ID, t1, t2)
                    no = XL_CTCaThe.No2(t.ID, t1, t2)
                End If
                HienThi(DS)
                Label4.Text = no
                MessageBox.Show("Thêm thanh cong!")
            Else
                MessageBox.Show("Thêm khong thanh cong!")
            End If
        Catch ex As Exception
            MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End Try
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
        DS.Clear()
        Dim no As Integer = 0
        Dim t1 As Date = DateTimePicker1.Value
        Dim t2 As Date = DateTimePicker2.Value
        DS = XL_CTCaThe.LayDS2(t.ID, t1, t2)
        no = XL_CTCaThe.No2(t.ID, t1, t2)
        HienThi(DS)
        Label4.Text = no
    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
        DS.Clear()
        Dim no As Integer = 0
        DS = XL_CTCaThe.LayDS(t.ID)
        no = XL_CTCaThe.No(t.ID)
        HienThi(DS)
        Label4.Text = no
    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        RadioButton2.Checked = False
    End Sub

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
        RadioButton2.Checked = False
    End Sub
End Class